import face_recognition
import argparse, os
import dlib
import matplotlib
import matplotlib.patches as patches
import matplotlib.pyplot as plt

parser = argparse.ArgumentParser(description="FaceRecognition")
parser.add_argument('--ReadImage_Way', default="/home/wks/users/zsy/face_recognition/examples/obama.jpg", type=str, help='Read Image')
parser.add_argument('--TargetImage_Way', default="/home/wks/users/zsy/face_recognition/examples/unknown.jpg", type=str, help='Read Image')
opt = parser.parse_args()

if __name__=="__main__":
    print("---加载已知图片")
    image = face_recognition.load_image_file(opt.ReadImage_Way)
    print("---加载完成")

    print("---定位图片中的人脸")
    face_locations = face_recognition.face_locations(image)
    print("---定位完成---")
    print("---坐标\n")
    print("Point01:" + str(face_locations[0][0]) + "," + str(face_locations[0][1]))
    print("Point02:" + str(face_locations[0][2]) + "," + str(face_locations[0][3]))
#    print("---使用opencv读取图像呈现结果")
#    print("---OpenCV加载图片中")
#    img = cv2.imread(r"/home/wks/users/zsy/face_recognition/examples/obama.jpg")
#    print("---OpenCV加载的图像大小:")
#    print(img.shape)
#    cv2.rectangle(img, (face_locations[0][0], face_locations[0][1]), (face_locations[0][2], face_locations[0][3]), (0, 255, 0), 2)
#    cv2.imshow("图像人脸检测结果", img)
    img = matplotlib.image.imread(opt.ReadImage_Way)
    plt.figure(1)
    plt.imshow(img)
    currentAxis = plt.gca()
    height = 0
    length = 0
    if face_locations[0][2] > face_locations[0][0]:
        height = face_locations[0][2] - face_locations[0][0]
    elif face_locations[0][0] > face_locations[0][2]:
        height = face_locations[0][2] - face_locations[0][0]
    else:
        print("坐标读取错误，无法选中目标区域")

    if face_locations[0][1] > face_locations[0][3]:
        length = face_locations[0][1] - face_locations[0][3]
    elif face_locations[0][1] > face_locations[0][3]:
        length = face_locations[0][1] - face_locations[0][3]
    else:
        print("坐标读取错误，无法选中目标区域")

    y = face_locations[0][0]
    x = face_locations[0][3]
#    print("{}.{}".format(height,length))
    rect = patches.Rectangle((x, y), length, height, linewidth=1, edgecolor='r', facecolor='none')
    currentAxis.add_patch(rect)



    print("---正在检测图中人脸关键点")
    face_landmarks_list = face_recognition.face_landmarks(image)
    print("---检测完成")
    print("人脸字典:\n{}".format(face_landmarks_list))
    plt.figure(2)
    img1 = matplotlib.image.imread(opt.ReadImage_Way)
    plt.imshow(img1)
    k = []
    for key in face_landmarks_list[0].keys():
        k.append(key)
    point_color = (0, 0, 255)
    thickness = 2
    for i in range(0,len(k)-1):
        for p in face_landmarks_list[0][k[i]]:
#            print("坐标为:({}.{})".format(p[0],p[1]))
            plt.scatter(p[0], p[1], color='r', s=5)
#            cv.circle(img, p, point_size, point_color, thickness)
#    cv2.imshow("图像人脸检测结果", img)

    print("---加载待检测图片")
    unknown_image = face_recognition.load_image_file(opt.TargetImage_Way)
    img2 = matplotlib.image.imread(opt.TargetImage_Way)
    print("---加载完成")

    print("---正在对待检测图片进行编码处理")
    obama_encoding = face_recognition.face_encodings(image)[0]
    unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
    print("---完成编码处理")

    print("---正在比对图片")
    results = face_recognition.compare_faces([obama_encoding],unknown_encoding)
    print("---比对完成")
    print("---检测结果")
    if results[0] == True:
        print("---检测人物与已知人物相符")
        plt.figure(3)
        plt.imshow(img2)
    else:
        print("---检测人物与已知人物不相符")
    plt.show()
#   cv2.imshow("已知目标", img)
#    cv2.imshow("未知目标", target)


